import copy from './copy'
import paste from './paste'
import del from './delete'
import movefront from './movefront'
import moveback from './moveback'
import movedown from './movedown'
import moveup from './moveup'
import records from './records'
import redo from './redo'
import undo from './undo'
import lock from './lock'
import unlock from './unlock'

var commands={
    data(){
        return{
            cmds:['copy','del','cut','movefront','moveback','moveup','movedown','records','redo','undo','unlock','lock'],
            cmdActive:{}
        }
    },    
    watch:{
        slectedCount(n,o){
            this.cmds.forEach(c=>{
                var cmd=`cmd_${c}_active`;
                if(this[cmd] && typeof(this[cmd]) === 'function'){
                    this[cmd](n);
                }
            });
        }
    },
    methods:{
        cmdo:function(c,args){
            // console.log(`commandDo:${c}`,args,this);
            var cmd=`cmd_${c}_do`;
            if(this[cmd] && typeof(this[cmd]) === 'function'){
                this[cmd](args);
            }else{
                console.warn(`命令[${c}]没有实现`);
            } 
        },
        commandKey:function(event){           
            // console.log('commandKey:',event);
            var key=event.key?event.key.toLowerCase():'';
            if(event.ctrlKey && key){
                switch(key){
                    case 'c':                        
                        this.cmdo('copy');
                        break;
                    case 'v':
                        this.cmdo('paste');
                        break;
                    case 'x':
                        this.cmdo('cut');
                        break;
                    case 'z':
                        this.cmdo('undo');
                        break;
                    case 'y':
                        this.cmdo('redo');
                        break;
                    case 'arrowup':
                        if(event.shiftKey){
                            this.cmdo('movefront');
                        }else{
                            this.cmdo('moveup');
                        }                        
                        break;
                    case 'arrowdown':
                        if(event.shiftKey){
                            this.cmdo('moveback');
                        }else{
                            this.cmdo('movedown');
                        }                        
                        break;
                    case 'l':
                        if(event.shiftKey){
                            this.cmdo('lock');
                        }else{
                            this.cmdo('unlock');
                        }                        
                        break;
                }
            }else if(key === 'delete'){
                this.cmdo('del');
            }
            
        }
    }
}

export default [copy,paste,del,movefront,moveback,movedown,moveup,records,redo,undo,unlock,lock,commands];